package com.oa.admin.controller;

import com.joneying.common.web.response.Resp;
import com.oa.admin.dto.CarAddDTO;
import com.oa.admin.dto.CarConditionDTO;
import com.oa.admin.dto.CarMaintatainDTO;
import com.oa.admin.dto.CarUpdateDTO;
import com.oa.admin.entity.Car;
import com.oa.admin.service.ICarService;
import com.oa.core.exception.BusinessException;
import com.oa.core.exception.DataIsEmptyException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.joneying.common.annotation.CommonController;
import org.springframework.context.annotation.Description;

import java.util.List;

import org.springframework.web.bind.annotation.RestController;
import com.oa.core.BaseController;
import springfox.documentation.annotations.ApiIgnore;

/**
 * @author syb123
 * @since 2019-01-22
 */
@RestController
@RequestMapping("/car")
@Api(description = "车俩表模块")
//@ApiIgnore
public class CarController extends BaseController {

    private final ICarService carService;

    @Autowired
    public CarController(ICarService carService) {
        this.carService = carService;
    }

    /**
     * 查询列表
     *
     * @return Resp
     */
    @GetMapping("/list")
    @CommonController
    @Description("查询所有")
    @ApiOperation(value = "查询（不传page则查询所有）")
    public Resp findList(@ApiParam("条件对象") CarConditionDTO carConditionDTO) throws DataIsEmptyException {
        return new Resp().success(carService.findCarListByCondition(carConditionDTO));
    }

    /**
     * 根据Id查询
     *
     * @param id id
     * @return Resp
     */
    @GetMapping("/{id}")
    @CommonController
    @Description("获取详细信息")
    @ApiOperation(value = "根据id查询")
    public Resp findById(@ApiParam(value = "id", required = true) @PathVariable String id) {
        return new Resp().success(carService.findCarDetailsById(id));
    }


    /**
     * 新增
     *
     * @param carAddDTO CarAddDTO
     * @return Resp
     */
    @PostMapping("")
    @CommonController
    @Description("新增")
    @ApiOperation(value = "新增")
    public Resp add(@ApiParam(value = "车俩表对象") @RequestBody(required = false) CarAddDTO carAddDTO) throws BusinessException {
        return carService.addCar(carAddDTO);
    }

    /**
     * 根据Id修改
     *
     * @param carUpdateDTO CarUpdateDTO
     * @return Resp
     */
    @PutMapping("")
    @CommonController
    @Description("根据id修改")
    @ApiOperation(value = "根据id修改")
    public Resp update(@ApiParam(value = "车俩表对象") @RequestBody(required = false) CarUpdateDTO carUpdateDTO) throws BusinessException {
        return carService.updateCarDetailsById(carUpdateDTO);
    }

    /**
     * 根据Id删除
     *
     * @param id id
     * @return Resp
     */
    @DeleteMapping("/{id}")
    @CommonController
    @Description("根据id删除")
    @ApiOperation(value = "根据id删除")
    public Resp delete(@ApiParam(value = "id", required = true) @PathVariable("id") String id) {
        Car car = carService.selectById(id);
        if (car != null && car.getIsDeleted() == 2) {
            car.setIsDeleted(1);
            if (carService.updateById(car)) {
                return new Resp().success();
            }
            return new Resp().failure();
        }
        return new Resp().failure();
    }

    @PostMapping("/maintain")
    @CommonController
    @Description("维修")
    @ApiOperation(value = "维修")
    public Resp carMaintain(@ApiParam("维修对象") CarMaintatainDTO carMaintatainDTO) throws BusinessException {
        return carService.carMaintain(carMaintatainDTO);
    }

    @PutMapping("/statusNormal")
    @CommonController
    @Description("修改车辆状态")
    @ApiOperation(value = "修改车辆状态")
    public Resp updateCarStatusNormal(@ApiParam("车辆ID") @RequestParam("id") String id) throws BusinessException {
        return carService.updateCarStatusNormal(id);
    }
}
